//
//  ChangePassWordViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/18.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "ChangePassWordViewController.h"

@interface ChangePassWordViewController (){
    UITextField *oldPassWordTextField;
    UITextField *newPassWordTextField;

}

@end

@implementation ChangePassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1.000];
    self.title = @"修改密码";
    [self setUI];
    
}

- (void)setUI{
    UILabel *oldPassWord = [[UILabel alloc]init];
    [self.view addSubview:oldPassWord];
    [oldPassWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(65*FitSize);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(30*FitSize);
    }];
    oldPassWord.text = @"旧密码:";
    oldPassWord.font = FONT(14);
    oldPassWord.textColor = [UIColor colorWithWhite:0.377 alpha:1.000];
    
    oldPassWordTextField = [[UITextField alloc]init];
    [self.view addSubview:oldPassWordTextField];
    oldPassWordTextField.backgroundColor = [UIColor whiteColor];
    oldPassWordTextField.placeholder = @"请输入旧密码 6~25个字符";
    oldPassWordTextField.textAlignment = NSTextAlignmentCenter;
    oldPassWordTextField.secureTextEntry = YES;
    oldPassWordTextField.clearButtonMode = YES;
    oldPassWordTextField.clearsOnBeginEditing = YES;
    [oldPassWordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(oldPassWord.mas_bottom).offset(5);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(30*FitSize);
    }];
    
    UILabel *newPassWord = [[UILabel alloc]init];
    [self.view addSubview:newPassWord];
    [newPassWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(oldPassWordTextField.mas_bottom).offset(10);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(30*FitSize);
    }];
    newPassWord.text = @"新密码:";
    newPassWord.font = FONT(14);
    newPassWord.textColor = [UIColor colorWithWhite:0.377 alpha:1.000];
    newPassWordTextField = [[UITextField alloc]init];
    newPassWordTextField.backgroundColor = [UIColor whiteColor];
    newPassWordTextField.placeholder = @"请输入新密码 6~25个字符";
    newPassWordTextField.textAlignment = NSTextAlignmentCenter;
    newPassWordTextField.secureTextEntry = YES;
    newPassWordTextField.clearButtonMode = YES;
    newPassWordTextField.clearsOnBeginEditing = YES;
    [self.view addSubview:newPassWordTextField];
    [newPassWordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(newPassWord.mas_bottom).offset(5);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(35*FitSize);
    }];
    
    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(newPassWordTextField.mas_bottom).offset(40);
        make.left.mas_equalTo(15*FitSize);
        make.right.mas_equalTo(-15*FitSize);
        make.height.mas_equalTo(30*FitSize);
    }];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(changePassWord) forControlEvents:UIControlEventTouchUpInside];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureButton.backgroundColor = [UIColor colorWithRed:0.061 green:0.479 blue:1.000 alpha:1.000];
    sureButton.layer.cornerRadius = 5;

}

- (void)changePassWord{
    if (oldPassWordTextField.text.length == 0) {
        [XHToast showCenterWithText:@"请输入旧密码"];
        return;
    }
   
    if (newPassWordTextField.text.length == 0) {
        [XHToast showCenterWithText:@"请输入新密码密码"];
        return;
    }

    [LBProgressHUD showHUDto:self.view animated:YES];
    NSString *ashx = @"/Server/UserHandler.ashx";
       NSDictionary *dic = @{@"act":@"changepwd",@"uname":[UserInfo getUserName],@"upwd":oldPassWordTextField.text,@"newpwd":newPassWordTextField.text};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        [LBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                [XHToast showCenterWithText:@"密码修改成功"];
                [UserInfo setPassWord:newPassWordTextField.text];
                [self.navigationController popViewControllerAnimated:YES];
       
            }else{
                
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            [XHToast showBottomWithText:@"请求失败"];
            
        }
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [oldPassWordTextField resignFirstResponder];
    [newPassWordTextField resignFirstResponder];

}

@end
